<?php
/**
 * design.class.php - Template functions
 *
 * In this file all template-related functionality is contained.
 */

class Design
{
	/**
	 * Load a template and wrap it into the page blueprint.
	 */
	static public function page($templateName, $vars = array())
	{
		Event::log('framework.design', 'Showing page with template '.$templateName);
		
		$vars['pageVars'] = $vars;
		$vars['pageTemplate'] = $templateName;
		
		self::load('design/blueprint.tpl.php', $vars);
	}
	
	/**
	 * Load a template.
	 *
	 * @param String $templateName
	 * @param Array $vars
	 */
	static public function load($templateName, $vars = array())
	{
		extract($vars);
		include(Config::get('path').$templateName);
	}
	
	/**
	 * Loads a template, but doesn't output it
	 *
	 * @param string $templateName
	 * @param array $vars
	 */
	static public function read($templateName, $vars = array())
	{
		ob_start();
		self::load($templateName, $vars);
		$result = ob_get_contents();
		ob_end_clean();
		return $result;
	}
	
	/**
	 * This function creates the code necessary to add a flash movie.
	 */
	static public function showFlash($name, $width = "100%", $height = "100%", $flashOptionsArray = array(), $flashVarsArray = array(), $minFlashVersion = '9,0,28')
	{
		// set options defaults
		$options = array(
			'quality' => 'high',
			'bgcolor' => '#000000',
			'allowScriptAccess' => 'sameDomain',
			'allowFullScreen' => 'true',
			'id' => $name
		);
		
		$options = array_merge($options, $flashOptionsArray);
		
		// encode flashVars
		$flashVars = ''; $first = true;
		foreach($flashVarsArray as $key => $value) {
			if(!$first) $flashVars .= '&'; else $first = false;
			$flashVars .= $key.'='.urlencode($value);
		}
		if (strlen($flashVars)) $options['flashVars'] = $flashVars;
		
		// prepare movie url
		$movie = Url::local('swf/'.$name);
		
		// prepare id
		$id = $options['id']; unset($options['id']);
		
		// send html to client
		self::load('flash', compact('name', 'id', 'movie', 'width', 'height', 'options', 'minFlashVersion'));
	}
}

?>
